import commonservice from "./commonservice.js"
//import {saveAs} from "../common/js/FileSaver.min.js"
import ajaxservice from "./ajaxservice.js"

const SchoolSearch = function(info) {
	return ajaxservice.webRequest({
		url:'/api/School/Search',
		method:'POST',
		data:info
	});
}

const SchoolGet = function(schoolId) {
	return ajaxservice.webRequest({
		url:"/api/School/" + schoolId,
		method:'GET'
	});
}

const SchoolAdd = function(info) {
	return ajaxservice.webRequest({
		url:"/api/School/Add",
		method:'POST',
		data:info
	});
}

const SchoolEdit = function(info) {
	return ajaxservice.webRequest({
		url:"/api/School/Edit",
		method:'PUT',
		data:info
	});
}

const SchoolDel = function(schoolId) {
	return ajaxservice.webRequest({
		url:"/api/School/BatchDelete",
		method:'POST',
		data:[schoolId]
	});
} 
const SchoolDels = function(schoolId) {
	return ajaxservice.webRequest({
		url:"/api/School/BatchDelete",
		method:'POST',
		data:schoolId
	});
} 

const ExportExcel = function(info) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: commonservice.WEBSITE + "/api/School/ExportExcel",
			header: {
				'Authorization': 'Bearer ' + commonservice.AccessToken
			},
			data: info,
			method: 'POST',
			responseType:'arraybuffer',
			success: function(res) {
				console.log(JSON.stringify(res))
				//var blob = res.data;
				 // saveAs(res.data, 'sdfassdfs.xls');
				 //           var reader = new FileReader();
				 
				 //           reader.readAsDataURL(blob);    // 转换为base64，可以直接放入a表情href
				 
				 //           reader.onload = function (e) {
				 
				 //               // 转换完成，创建一个a标签用于下载
				 
				 //               var a = document.createElement('a');
				 
				 //               a.download = 'data.csv';
				 
				 //               a.href = e.target.result;
				 
				 //               $("body").append(a);    // 修复firefox中无法触发click
				 
				 //               a.click();
				 
				 //               $(a).remove();
				 
				 //           }
				resolve(res);
				
			},
			fail: function(ex) {
				uni.showToast({
					title: '网络异常',
					icon: 'none'
				});
			}
		})
	})
}
///api/School/ExportExcelByIds
const ExportExcelByIds=function(schoolIds){
	console.log(schoolIds)
	return new Promise((resolve,reject)=>{
		uni.request({
			url:commonservice.WEBSITE+"/api/School/ExportExcelByIds",
			header:{
				'Authorization':'Bearer '+commonservice.AccessToken
			},
			data:schoolIds,
			method: 'POST',
			success:function(res){
				resolve(res);
			},
			fail:function(ex){
				uni.showToast({
					title: '网络异常',
					icon:'none'
				});
			}
		})
	})
}

const SchoolImprot=function(info){
	return ajaxservice.webRequest({
		url:"/api/School/Import",
		method:'POST',
		data:info
	});
}
export default {
	SchoolSearch: SchoolSearch, //根据学校名称搜索
	SchoolGet: SchoolGet, //获取学校信息
	SchoolAdd: SchoolAdd, //添加学校
	SchoolEdit: SchoolEdit, //编辑学校
	SchoolDel: SchoolDel, //删除学校
	SchoolDels:SchoolDels,//批量删除
	ExportExcel: ExportExcel, //根据条件下载文件
	ExportExcelByIds:ExportExcelByIds, //根据选项下载
	SchoolImprot:SchoolImprot  //导入学校 数据
}
